package rest.o.gram.location;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import android.location.LocationManager;
import android.os.Looper;
import android.os.Handler;

/**
 * Created with IntelliJ IDEA.
 * User: Roi
 * Date: 17/04/13
 */
public class LocationTrackerDummy implements ILocationTracker {

    public LocationTrackerDummy() {
        isTracking = false;
        timer = new Timer();
        task = new GetLocationTimerTask();
        location = new DummyLocation();
    }

    @Override
    public void start() {
        if(isTracking)
            stop();

        timer.schedule(task, 2000);
    }

    @Override
    public void stop() {
        if(!isTracking)
            return;

        timer.cancel();
        isTracking = false;
    }

    @Override
    public Location estimate() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Location getEstimation() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void setObserver(ILocationObserver observer) {
        this.observer = observer;
    }

    class GetLocationTimerTask extends TimerTask {
        private Handler mHandler = new Handler(Looper.getMainLooper());

        @Override
        public void run() {
            // ...
            mHandler.post(new Runnable() {
                public void run() {
                    observer.onLocationUpdated(location);
                    stop();
                }
            });
        }
    }

    class DummyLocation implements Location {

        @Override
        public long getLastUpdateTimestamp() {
            return Calendar.getInstance().getTimeInMillis() - 30;
        }

        @Override
        public long getLastBroadcastTimestamp() {
            return Calendar.getInstance().getTimeInMillis() - 30;
        }

        @Override
        public float getLastLat() {
            return 32.078145f;
        }

        @Override
        public float getLastLong() {
            return 34.781449f;
        }

        @Override
        public int getLastAccuracy() {
            return 2;
        }

        @Override
        public String getLastOriginProvider() {
            return LocationManager.GPS_PROVIDER;
        }

        @Override
        public boolean anyLocationDataReceived() {
            return true;
        }

        @Override
        public boolean anyLocationDataBroadcast() {
            return true;
        }

        @Override
        public boolean hasLatestDataBeenBroadcast() {
            return true;
        }

        @Override
        public long getTimestampAgeInSeconds() {
            return 30;
        }
    }

    private ILocationObserver observer;
    private Timer timer;
    private GetLocationTimerTask task;
    private boolean isTracking;
    private Location location;
}